import { ElMessage } from 'element-plus';
import { useClipboard } from '@vueuse/core';
import { throttle } from 'lodash-es';

export const useCopy = (options?: any) => {
  const { message } = options ?? {};
  const { copy, isSupported } = useClipboard({ legacy: true });

  const onCopy = throttle(async (text: string, msg?: string) => {
    if (!isSupported.value) return ElMessage.success('当前浏览器不支持复制，请更换浏览器');

    await copy(text);
    ElMessage.success(msg || message || '复制成功');
  });

  return { onCopy };
};
